package cn.gzjp.modules.dt.api.web;

import cn.gzjp.common.web.BaseController;
import cn.gzjp.modules.dt.api.bean.ExamQuestionVo;
import cn.gzjp.modules.dt.entity.DtUser;
import cn.gzjp.modules.dt.entity.Question;
import cn.gzjp.modules.dt.entity.TestRecord;
import cn.gzjp.modules.dt.service.DtUserService;
import cn.gzjp.modules.dt.service.QuestionService;
import cn.gzjp.modules.dt.service.TestRecordService;
import cn.gzjp.modules.it.api.bean.CommonJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author zengcp
 * @Date 2018/2/24
 */
/**
 * 顺序练习管理
 */
@RestController("driveTraining")
@Api(value = "练习模式", description = "练习模式")
@RequestMapping(value = "${frontPath}/training")
public class DriveTrainingController extends BaseController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private TestRecordService testRecordService;
    @Autowired
    private DtUserService dtUserService;


    /**
     * 开始练习
     */
    @ApiOperation(value = "开始练习")
    @RequestMapping(value = "start", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
    })
    public String start(String openid) {
        log.info("开始练习 openid="+openid);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        DtUser user = dtUserService.get(openid);
        if(user==null){
            user = new DtUser();
            user.setId(openid);
            dtUserService.save(user);
        }
        Question question = questionService.get(user.getTestSn());
        if(question==null){
            result.setCode(-1);
            result.setMsg("题目不存在");
        }
        ExamQuestionVo data = new ExamQuestionVo();
        question.setSn(user.getTestSn());
        data.setQuestion(question);
        data.setMaxSn(questionService.getCount());
        data.setCorrectCount(testRecordService.getCorrectCount(openid));
        data.setTotalCount(testRecordService.getTotalCount(openid));
        data.setErrorCount(testRecordService.getErrorCount(openid));
        result.setData(data);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 考试题目获取
     */
    @ApiOperation(value = "题目获取")
    @RequestMapping(value = "question", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true),
            @ApiImplicitParam(name="sn",value="题目编号",dataType="Integer",paramType="query",required =true)
    })
    public String question(String openid,Integer sn) {
        log.info("练习题目获取 openid="+openid+"|sn="+sn);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        DtUser user = dtUserService.get(openid);
        if(user==null){
            user = new DtUser();
            user.setId(openid);
            dtUserService.save(user);
        }
        Question question = questionService.get(sn);
        if(question==null){
            result.setCode(-1);
            result.setMsg("题目不存在");
        }
        ExamQuestionVo data = new ExamQuestionVo();
        question.setSn(sn);
        data.setQuestion(question);
        data.setMaxSn(questionService.getCount());
        data.setCorrectCount(testRecordService.getCorrectCount(openid));
        data.setTotalCount(testRecordService.getTotalCount(openid));
        data.setErrorCount(testRecordService.getErrorCount(openid));
        result.setData(data);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 图标题目列表
     */
    @ApiOperation(value = "图标题目列表获取")
    @RequestMapping(value = "picQuestionList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
    })
    public String picQuestionList(String openid) {
        log.info("图标题目列表 openid="+openid);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        DtUser user = dtUserService.get(openid);
        if(user==null){
            user = new DtUser();
            user.setId(openid);
            dtUserService.save(user);
        }
        Question search = new Question();
        search.setPic("yes");
        List<Question> data = questionService.findList(search);
        result.setData(data);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 随机题目获取
     */
    @ApiOperation(value = "随机题目获取")
    @RequestMapping(value = "random", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
    })
    public String random(String openid) {

        int maxSn=questionService.getCount();
        int minSn=1;
        Random random = new Random();
        int sn = random.nextInt(maxSn)%(maxSn-minSn+1) + minSn;
        log.info("随机题目获取 openid="+openid+"|sn="+sn);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        DtUser user = dtUserService.get(openid);
        if(user==null){
            user = new DtUser();
            user.setId(openid);
            dtUserService.save(user);
        }
        Question question = questionService.get(sn);
        if(question==null){
            result.setCode(-1);
            result.setMsg("题目不存在");
        }
        ExamQuestionVo data = new ExamQuestionVo();
        question.setSn(sn);
        data.setQuestion(question);
        data.setMaxSn(maxSn);
        data.setCorrectCount(testRecordService.getCorrectCount(openid));
        data.setTotalCount(testRecordService.getTotalCount(openid));
        data.setErrorCount(testRecordService.getErrorCount(openid));
        result.setData(data);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 练习答题
     * @param openid
     * @param sn
     * @param option
     * @return
     */
    @ApiOperation(value = "答题")
    @RequestMapping(value = "answer", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true),
            @ApiImplicitParam(name="sn",value="题目编号",dataType="Integer",paramType="form",required =true),
            @ApiImplicitParam(name="option",value="答案选项",dataType="String",paramType="form",required =true),
    })
    public String answer(String openid, Integer sn, String option) {
        log.info("练习答题 openid="+openid+"|sn="+sn+"|option="+option);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        DtUser user = dtUserService.get(openid);
        if(user==null){
            user = new DtUser();
            user.setId(openid);
            dtUserService.save(user);
        }
        Question question = questionService.get(sn);
        option = option.replaceAll(",", "");
        char[] chars = option.toString().toCharArray();
        Arrays.sort(chars);
        String newAnswer = new String(chars);

        TestRecord record = testRecordService.findByOpenidAndQuestionId(openid,sn);

        if(record==null){
            record = new TestRecord();
        }
        record.setAnswer(newAnswer);
        record.setQuestion(question);
        record.setOpenid(openid);
        record.setResult("1");
        record.setCreateTime(new Date());
        if(question.getAnswer().equals(newAnswer)){
            record.setResult("0");
            result.setMsg("恭喜你,答对了！");
            user.setTestCorrectCount(user.getTestCorrectCount()+1);
        }else {
            result.setCode(1);
            result.setMsg("很遗憾,答错了");
        }
        user.setTestCount(user.getTestCount()+1);
        user.setTestSn(sn);
        dtUserService.save(user);
        testRecordService.save(record);
        result.setData(sn);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 我的错题
     */
    @ApiOperation(value = "我的错题")
    @RequestMapping(value = "wdct", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
    })
    public String wdct(String openid) {
        log.info("我的错题获取 openid="+openid);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        TestRecord search = new TestRecord();
        search.setOpenid(openid);
        search.setResult("1");
        List<TestRecord> uncorrectList = testRecordService.findList(search);
        result.setData(uncorrectList);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 我的答题
     */
    @ApiOperation(value = "我的答题")
    @RequestMapping(value = "wddt", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
    })
    public String wddt(String openid) {
        log.info("我的答获取 openid="+openid);
        CommonJson result = new CommonJson();
        if (StringUtils.isBlank(openid)||openid.length()<12) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        TestRecord search = new TestRecord();
        search.setOpenid(openid);
        List<TestRecord> uncorrectList = testRecordService.findList(search);
        result.setData(uncorrectList);
        log.info("返回数据="+mapper.toJson(result));
        return this.mapper.toJson(result);
    }
}
